package com.om.common;

import java.sql.SQLException;
import java.util.ArrayList;




import java.util.List;

import com.om.base.AuthorizationCache;
import com.om.base.DataRow;
import com.om.base.Utils;

public abstract class BaseAuthorizationUser extends BaseBean {

	public final static int USER_NOT_EXITS = 1;
	public final static int PASSWORD_ERROR = 2;
	public final static int VAILDATECODE_ERROR = 3;
	public final static int LOGIN_SUCCESS = 0;

	protected String roles = "";
	protected String power = "";
	protected ArrayList<String> powerList = null;

	protected boolean loaded = false;

	protected abstract void loadUserInfo(String userName);

	protected abstract String loadPowers();

	protected abstract String loadRoles() throws SQLException;

	public abstract boolean checkVaildateCode();

	public abstract ArrayList<String> getPowerList();
	

	protected abstract ArrayList<String> loadPowerList();

	public abstract boolean checkUser(String userName) throws Exception;

	public abstract boolean checkPasswrod(String userName, String password);

	public abstract String getUserName();

	public abstract Object getUserID();

	public abstract String getPassword();

	public String getUserPowers() {
		return power;
	}

	public String getUserRoles() {
		return roles;
	}

	

	public synchronized int login(String userName, String password)
			throws Exception {

		if (this.connection == null) {
			this.connection = Utils.GetOpenedConnection();
		}

		if (!checkUser(userName)) {
			return BaseAuthorizationUser.USER_NOT_EXITS;
		}

		if (!checkPasswrod(userName, password)) {
			return BaseAuthorizationUser.USER_NOT_EXITS;
		}

		if (!checkVaildateCode()) {
			return BaseAuthorizationUser.VAILDATECODE_ERROR;
		}

		if (!loaded) {
			loadUserInfo(userName);
		}
		
		afterLogin();
		
		//cacheLoginUser();
		
		return BaseAuthorizationUser.LOGIN_SUCCESS;

	}
	
	public void afterLogin() throws Exception{
		
		roles = loadRoles();

		power = loadPowers();

		powerList = loadPowerList();

		
	}
	
}
